package cn.net.yugu.doraemon.common.jackson.serializer;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * 整数【分】数值转为 【元】
 *
 * @author xiangzr
 * @version 2023-03-08 13:32:00
 */
public class IntCentMoneyJsonSerializer extends JsonSerializer<Integer> {

    public static final BigDecimal HUNDRED = BigDecimal.valueOf(100);
    public static final String YAN_PLAIN = "元";

    @Override
    public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value != null) {
            String plainString = new BigDecimal(value)
                    .divide(HUNDRED, 2, RoundingMode.HALF_DOWN)
                    .toPlainString();

            gen.writeString(plainString + " " + YAN_PLAIN);
        }
    }
}
